package controllers;

import java.util.ArrayList;
import java.util.List;

import com.avaje.ebean.*;
import controllers.security.SecuredUser;
import models.application.*;
import models.frame.*;
import models.pubobj.*;
import models.pubobj.AniObjectState.*;
import models.pubobj.AniObjectType;
import models.user.*;
import models.user.AniUserRole.*;
import play.Logger;
import play.libs.*;
import play.mvc.*;
import utils.*;
import dto.retjson.*;

public class BaseController extends Controller {
    //scripts
	public static Result initdb(){
		//role
		for(RoleKeys oneRoleKey: AniUserRole.RoleKeys.values()){
			AniUserRole oneRole = new AniUserRole(oneRoleKey.name());
			oneRole.save();
		}
    	//state
    	for(AniObjectState.States oneStateKey: AniObjectState.States.values()){
            Logger.info(oneStateKey.name());
    		AniObjectState oneState = new AniObjectState(oneStateKey.name());
    		oneState.save();
    	}
		//test user account
    	AniUser oneUser = new AniUser();
    	oneUser.email = "ching-zhou@anicloud.com";
    	oneUser.password = SecureUtils.generateMD5String("lucky19920803");
    	oneUser.role = AniUserRole.findByRoleKey(RoleKeys.ANI_ADMIN.name());
    	oneUser.screenName = "Yeh Ching-zhou";
    	
    	
    	oneUser.state = AniObjectState.findByStateKey(States.ACTIVATED.name());
    	
    	oneUser.avatar = "anicloud".getBytes();
    	oneUser.save();
    	
    	//method
    	for(HttpUtils.HttpMethods oneMethodKey: HttpUtils.HttpMethods.values()){
    		AniHttpMethod oneMethod = new AniHttpMethod(oneMethodKey.name());
    		oneMethod.save();
    	}
        //app type
        List<AniObjectType> appTypes = new ArrayList<AniObjectType>();
        for(AniObjectType.ObjectTypes oneEnumType: AniObjectType.ObjectTypes.values()){
            appTypes.add(new AniObjectType(oneEnumType.name()));
        }
        Ebean.save(appTypes);
        //app classification
    	List<AniAppClassification> appClassifications = new ArrayList<AniAppClassification>();
        for(AniAppClassification.AppClassifications oneClass: AniAppClassification.AppClassifications.values()){
            appClassifications.add(new AniAppClassification(oneClass.name()));
        }
        Ebean.save(appClassifications);

    	//menu
        //-home menu
    	AniMenuObj home = new AniMenuObj(
    			1L,
    			"DASHBOARD",
    			"BaseController",
    			"index",
    			AniObjectState.findByStateKey(States.ACTIVATED.name()),
    			null
    			);
    	home.save();
        //-app menu
    	AniMenuObj appMenuRoot = new AniMenuObj(
                2L,
                "APP",
                null,
                null,
                AniObjectState.findByStateKey(States.ACTIVATED.name()),
                null
        );
        appMenuRoot.save();
        List<AniMenuObj> appChildren = new ArrayList<AniMenuObj>();
        appChildren.add(new AniMenuObj(
                null,
                "INSTALLED",
                "FrontEndFrameController",
                "installedAppsList",
                AniObjectState.findByStateKey(States.ACTIVATED.name()),
                appMenuRoot

        ));
        appChildren.add(new AniMenuObj(
                null,
                "MY_APPS",
                "FrontEndFrameController",
                "userOwnedAppsList",
                AniObjectState.findByStateKey(States.ACTIVATED.name()),
                appMenuRoot

        ));
        appChildren.add(new AniMenuObj(
                null,
                "REGISTER",
                "FrontEndFrameController",
                "appRegister",
                AniObjectState.findByStateKey(States.ACTIVATED.name()),
                appMenuRoot

        ));

        Ebean.save(appChildren);
        return ok(Json.toJson(new RetDataDto(true, "dbinit", oneUser)));
	}
	
	public static Result testUtil(){
		
		String retStr = null;
		try {
            retStr = "ok";
            List<AniApiParam> apiParams = AniApi.getById(2L).params;
            apiParams.get(0).paramName = "test";
            Ebean.save(apiParams);
        } catch (Exception e) {
			e.printStackTrace();
			retStr = "[ERROR]" + e.getMessage();
		}
		return ok(retStr);
	}

    @Security.Authenticated(SecuredUser.class)
    public static Result index() {
    	List<AniUser> users = AniUser.findAll();
    	return ok(Json.toJson(new RetDataDto(true, "aniHomepageTest", users)));
    }
	
	public static Result test(){
//		Map<String, String> params = new HashMap<String, String>();
//		String result = HttpUtils.callUrlSync("http://localhost", HttpUtils.HttpMethods.GET.name(), params);
//		return ok(result);

        return ok();
	}
}
